﻿using Microsoft.Xna.Framework;
using System.ComponentModel;

namespace SoulDoorEngine.Framework.Components
{
    /// <summary>
    /// 三维组件，在二维组件前绘制
    /// </summary>
    public interface I3DComponent
    {
        /// <summary>
        /// 边框盒
        /// </summary>
        [Browsable(false)]
        BoundingBox BoundingBox { get; }
        
        /// <summary>
        /// X、Y、Z三轴的缩放
        /// </summary>
        [Category("3D Component")]
        Vector3 Scale { get; set; }
        
        /// <summary>
        /// 旋度，矩阵形式更易于计算
        /// </summary>
        [Browsable(false)]
        Matrix Rotation { get; set; }
        
        /// <summary>
        /// 欧拉旋度，3维向量难以计算，保留以提供简便的手动修改功能
        /// </summary>
        //[DisplayName("Rotation")]//2012.02.27注释，原因：WP7不兼容
        [Category("3D Component")]
        Vector3 EulerRotation { get; set; }
        
        /// <summary>
        /// 位置，由笛卡尔坐标表示
        /// </summary>
        [Category("3D Component")]
        Vector3 Position { get; set; }        
    }

    /// <summary>
    /// 二维组件，在三维组件后绘制
    /// </summary>
    public interface I2DComponent
    {
        /// <summary>
        /// 组件所在矩形
        /// </summary>
        Rectangle Rectangle { get; set; }     
    }

    /// <summary>
    /// 组件类型枚举，依次为：2D组件、3D组件、2D或3D组件、无视类型组件
    /// </summary>
    public enum ComponentType
    {
        Component2D,
        Component3D,
        Both,
        All
    }

    /// <summary>
    /// 让屏幕知道它拥有的组件引擎是否允许绘制，提供一个抽象基类
    /// </summary>
    public abstract class ComponentPredicate
    {
        public abstract bool IsComponentEligible(Component component);
    }

    public class ComponentTypePredicate : ComponentPredicate
    {
        private ComponentType _Type;

        /// <summary>
        /// 引擎支持绘制的组件类型
        /// </summary>
        public ComponentType Type
        {
            get { return _Type; }
            set { _Type = value; }
        }

        public ComponentTypePredicate(ComponentType type)
        {
            this.Type = type;
        }

        /// <summary>
        /// 判断一个组件是否合法
        /// </summary>
        /// <param name="component">用于判断的组件</param>
        /// <returns></returns>
        public override bool IsComponentEligible(Component component)
        {
            if (this.Type == ComponentType.Both)
            {
                if (component is I2DComponent || component is I3DComponent)
                {
                    return true;
                }
            }
            else if (this.Type == ComponentType.Component2D)
            {
                if (component is I2DComponent)
                {
                    return true;
                }
            }
            else if (this.Type == ComponentType.Component3D)
            {
                if (component is I3DComponent)
                {
                    return true;
                }
            }
            else if (this.Type == ComponentType.All)
            {
                return true;
            }

            return false;
        }
    }

}
